import { useEffect } from "react";

const { ipcRenderer } = window.require("electron");

export function useIpcRendererOn(eventMaps) {
  useEffect(() => {
    Object.keys(eventMaps).forEach((eventName) => {
      ipcRenderer.on(eventName, eventMaps[eventName]);
    });

    return () => {
      Object.keys(eventMaps).forEach((eventName) => {
        ipcRenderer.removeListener(eventName, eventMaps[eventName]);
      });
    };
  });
}

export function useIpcRendererEmit(eventNames = []) {
  useEffect(() => {
    eventNames.forEach((name) => {
      ipcRenderer.send(name);
    });
  }, []);
}
